package com.lz.controller.xjl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lz.entity.*;
import com.lz.service.xjl.EquipmentSheetService;

import com.lz.service.xjl.UpholsteryTypeService;
import com.lz.vo.xjl.ModifyEquipmentSheetVO;
import com.lz.vo.xjl.SelectEquipmentSheetVO;
import com.lz.vo.xjl.ViewEquipmentSheetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import util.PageHandleUtil;
import util.PageUitl;
import util.SendCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 徐建林
 * @date 2018/10/21 21:22
 * 装潢单webController
 */
@Controller
public class EquipmentSheetController {
    @Autowired
    private EquipmentSheetService equipmentSheetService;
    @Autowired
    private UpholsteryTypeService upholsteryTypeService;

    /**取消订单*/
    @RequestMapping(value = "cancelOrder")
    public String cancelOrder(Integer equipmentSheetNO ){
        //取消指定单号的
        equipmentSheetService.cancelEquipmentSheet(equipmentSheetNO);


        return "redirect:/equipmentList";
    }
    /**取消订单*/
    @RequestMapping(value = "cancelOrderHandle")
    public String cancelOrderHandle(Integer equipmentSheetNO ){
        //取消指定单号的
        equipmentSheetService.cancelEquipmentSheet(equipmentSheetNO);


        return "redirect:/handleEquipmentList";
    }
    @RequestMapping(value = "equipmentList")
    public String getEquipmentsList(Model model) {



       //查询所有装潢项目
        List<UpholsteryType> upholsteryTypeList = upholsteryTypeService.getUpholsteryTypeList();
        Map<String,Object> map=new HashMap<>();
        map.put("equipmentSheetNo","");
        map.put("customerName","");
        map.put("vin","");
        map.put("carCard","");
        map.put("upholsteryType","-1");
        map.put("openingTime1","");
        map.put("openingTime2","");
        map.put("equipmentSheetState","-1");


        Integer pageNo=1;
        Integer pageSize=3;
        //查询EquipmentSheet

        PageUitl pageUitl = new PageUitl(pageNo,pageSize,equipmentSheetService.getRows(map));
        List list = equipmentSheetService.getEquipmentSheetList
                (pageUitl.getPageNo(),pageUitl.getPageSize(), map);
        pageUitl.setList(list);
        model.addAttribute("pageUtil",pageUitl);
        model.addAttribute("upholsteryTypeList",upholsteryTypeList);
        model.addAttribute("map",map);
        //创建map存储回显条件

        return "decorationOrder";

    }
    /**条件查询*/
    @RequestMapping(value = "/equipmentList1",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getEquipmentsList1(@RequestBody SelectEquipmentSheetVO selectEquipmentSheetVO) {
        Map<String,Object> map=new HashMap<>();
        map.put("equipmentSheetNo",selectEquipmentSheetVO.getEquipmentSheetNo());
        map.put("customerName",selectEquipmentSheetVO.getCustomerName());
        map.put("vin",selectEquipmentSheetVO.getVin());
        map.put("carCard",selectEquipmentSheetVO.getCarCard());
        map.put("upholsteryType",selectEquipmentSheetVO.getUpholsteryType());
        map.put("openingTime1", selectEquipmentSheetVO.getOpeningTime1());
        map.put("openingTime2", selectEquipmentSheetVO.getOpeningTime2());
        map.put("equipmentSheetState",selectEquipmentSheetVO.getEquipmentSheetState());
        Integer pageNo = selectEquipmentSheetVO.getPageNo();

        if (pageNo==null){
            pageNo=1;
        }

       Integer pageSize=3;
        //查询装潢单的最大条数
        PageUitl pageUitl = new PageUitl(pageNo,pageSize,equipmentSheetService.getRows(map));
        //查询EquipmentSheet
        List list = equipmentSheetService.getEquipmentSheetList
                (pageUitl.getPageNo(),pageUitl.getPageSize(), map);
        pageUitl.setList(list);
        return JSON.toJSONString(pageUitl);

    }
    /**条件查询*/
    @RequestMapping(value = "/equipmentList",method = RequestMethod.POST)

    public String getEquipmentsList2(HttpServletRequest request,Model model) {
        Map<String,Object> map=new HashMap<>();
        map.put("equipmentSheetNo",request.getParameter("equipmentSheetNo"));
        map.put("customerName",request.getParameter("customerName"));
        map.put("vin",request.getParameter("vin"));
        map.put("carCard",request.getParameter("carCard"));
        map.put("upholsteryType",request.getParameter("upholsteryType"));
        map.put("openingTime1", request.getParameter("openingTime1"));
        map.put("openingTime2", request.getParameter("openingTime2"));
        map.put("equipmentSheetState",request.getParameter("equipmentSheetState"));
        String pageNo1 = request.getParameter("pageNo");
        Integer pageNo=null;
        if (pageNo1==null||"".equals(pageNo1)){
            pageNo=1;}else {
            pageNo=Integer.parseInt(pageNo1);
        }

        Integer pageSize=3;
        //查询装潢单的最大条数
        PageUitl pageUitl = new PageUitl(pageNo,pageSize,equipmentSheetService.getRows(map));
        //查询EquipmentSheet
        List list = equipmentSheetService.getEquipmentSheetList
                (pageUitl.getPageNo(),pageUitl.getPageSize(), map);
        pageUitl.setList(list);
        //查询所有装潢项目
        List<UpholsteryType> upholsteryTypeList = upholsteryTypeService.getUpholsteryTypeList();
       model.addAttribute("pageUtil",pageUitl);
       model.addAttribute("map",map);
       model.addAttribute("upholsteryTypeList",upholsteryTypeList);
       return "decorationOrder";


    }



    @RequestMapping(value = "/getEquipmentDetail",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getEquipment(String equipmentSheetNo) {
        //获取所有的安装人员list
        List<InstallationPerson> installationPersonList = upholsteryTypeService.getInstallationPersonList();
        List<TemporaryCarParts> temporaryCarPartsList = upholsteryTypeService.getTemporaryCarPartsList(Integer.parseInt(equipmentSheetNo));
        EquipmentSheet equipmentSheet=equipmentSheetService.getEquipmentById(Integer.parseInt(equipmentSheetNo));
        ViewEquipmentSheetVO equipmentSheetVO = new ViewEquipmentSheetVO(equipmentSheet,installationPersonList,temporaryCarPartsList);
        System.out.println(equipmentSheetVO);
        return JSON.toJSONString(equipmentSheetVO);

        }

    @RequestMapping("/insertEquipment")
    public String insertEquipment(Model model,HttpSession session) {
        //判断session中username是否存在
       if (session.getAttribute("employee")!=null){
           Employee employee = (Employee) session.getAttribute("employee");
           model.addAttribute("employeeName",employee.getEmployeeName());
           model.addAttribute("employeeId",employee.getEmployeeId());
       }
        //将Equipment的单号存入model
        Integer equipmentSheetNo = equipmentSheetService.getMaxPrimaryKey() + 1;
        //获取当前最大的顾客Id
        Integer customerId=upholsteryTypeService.getMaxcustomerNo()+1;
        //将Equipment的类型存入model
        List<UpholsteryType> upholsteryTypeList = upholsteryTypeService.getUpholsteryTypeList();
        //查询所有接单人
        model.addAttribute("customerId",customerId);
        model.addAttribute("equipmentSheetNo",equipmentSheetNo);
        model.addAttribute("upholsteryTypeList",upholsteryTypeList);



        return "insertEquipment";

    }

    /**新增一个Equipment*/
    @RequestMapping(value = "insertEquipment1",method = RequestMethod.POST)
    public String insertEquipment1(HttpServletRequest request) {
        String equipmentSheetNo = request.getParameter("equipmentSheetNo");
        String upholsteryType = request.getParameter("upholsteryType");
        String equipmentSheetState = request.getParameter("equipmentSheetState");
        String comment = request.getParameter("comment");
        String customerId = request.getParameter("customerId");
        String customerName = request.getParameter("customerName");
        String customerTel = request.getParameter("customerTel");
        String series = request.getParameter("series");
        String model = request.getParameter("model");
        String color = request.getParameter("color");
        String vin = request.getParameter("vin");
        String carCard = request.getParameter("carCard");
        String orderId = request.getParameter("orderId");
        String startTime = request.getParameter("startTime");
        String endingTime = request.getParameter("endingTime");
        Car car=new Car();
        Customer customer=new Customer();
        UpholsteryType type = new UpholsteryType();
        EquipmentSheet equipmentSheet = new EquipmentSheet();
        OrderSheet orderSheet = new OrderSheet();
        equipmentSheet.setEquipmentSheetNo(Integer.parseInt(equipmentSheetNo));
        equipmentSheet.setStartTime(new Date());
        equipmentSheet.setEndingTime(new Date());
        orderSheet.setOrderId(Integer.parseInt(orderId));
        type.setUpholsteryType(upholsteryType);
        equipmentSheet.setEquipmentSheetState(equipmentSheetState);
        equipmentSheet.setComment(comment);
        customer.setCustomerId(Integer.parseInt(customerId));
        customer.setCustomerName(customerName);
        customer.setCustomerTel(customerTel);

        car.setSeries(series);
        car.setModel(model);
        car.setColor(color);
        car.setVin(vin);
        car.setCarCard(carCard);





        return "insertEquipment";

    }

    @RequestMapping(value = "insertEquipment",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String insertEquipment(@RequestBody Map<String,String> map) {
        String u = map.get("upholsteryType");
        UpholsteryType upholsteryType = JSONObject.parseObject(u, UpholsteryType.class);
        String e = map.get("equipmentSheet");
        EquipmentSheet equipmentSheet = JSONObject.parseObject(e, EquipmentSheet.class);
        String emp = map.get("employee");
        Employee employee = JSONObject.parseObject(emp, Employee.class);
        String c = map.get("customer");
        Customer customer = JSONObject.parseObject(c, Customer.class);
        String carTemp = map.get("car");
        Car car = JSONObject.parseObject(carTemp, Car.class);
        String temporaryLIst = map.get("temporaryCarPartsLIst");
        List<TemporaryCarParts> temporaryCarPartsList  = JSONArray.parseArray(temporaryLIst, TemporaryCarParts.class);
        Integer i = equipmentSheetService.saveEquipmentSheet(equipmentSheet, car, customer, temporaryCarPartsList,employee,upholsteryType);
        return JSON.toJSONString(i);

    }
    /**减少一个equipmentSheet*/
    @RequestMapping(value = "removeEquipment")
    public String removeEquipment(Integer equipmentSheetNo) {
        Integer i=equipmentSheetService.removeEquipmentByNo(equipmentSheetNo);
        return null;
    }

    /**修改一个equipmentSheet*/

    @RequestMapping(value = "/modifyEquipment",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String modifyEquipmentSheet(@RequestBody ModifyEquipmentSheetVO modifyEquipmentSheetVO){

        equipmentSheetService.modifyEquipment(modifyEquipmentSheetVO);

        return JSON.toJSONString("已经成功修改!");
    }
    /**处理订单*/
    @RequestMapping(value = "/handleEquipmentList")
    public String handleEquipmentsList(Model model, @RequestParam(value="pageNo1", required=false)Integer pageNo1,@RequestParam(value="pageNo2", required=false) Integer pageNo2,@RequestParam(value="state", required=false) String state) {


        //判断session中username是否存在
       /* if (session.getAttribute("username")==null){
            return "/login";
        }*/

        Map<String,Object> map1=new HashMap<>();
        map1.put("equipmentSheetNo","");
        map1.put("customerName","");
        map1.put("vin","");
        map1.put("carCard","");
        map1.put("upholsteryType","-1");
        map1.put("openingTime1","");
        map1.put("openingTime2","");
        map1.put("equipmentSheetState","审核通过");
        Map<String,Object> map2=new HashMap<>();
        map2.put("equipmentSheetNo","");
        map2.put("customerName","");
        map2.put("vin","");
        map2.put("carCard","");
        map2.put("upholsteryType","-1");
        map2.put("openingTime1","");
        map2.put("openingTime2","");
        map2.put("equipmentSheetState","审核驳回");





        //查询EquipmentSheet
        PageHandleUtil pageUtil=new PageHandleUtil(pageNo1,3,equipmentSheetService.getRows(map1),pageNo2,3,equipmentSheetService.getRows(map2));

        List list1 = equipmentSheetService.getEquipmentSheetList
                (pageUtil.getPageNo1(),pageUtil.getPageSize1(), map1);
        pageUtil.setList1(list1);


        List list2 = equipmentSheetService.getEquipmentSheetList
                (pageUtil.getPageNo2(),pageUtil.getPageSize2(), map2);
        pageUtil.setList2(list2);
        model.addAttribute("pageUtil",pageUtil);
        model.addAttribute("state",state);


        //创建map存储回显条件

        return "handleOrder";

    }

    /**发送短信*/
    @RequestMapping(value = "sendMessageCode")
    public String sendMessageCode(String tel,Integer pageNo1,Integer pageNo2,Integer equipmentSheetNo) throws Exception {
        SendCode.msg(tel);
        //将状态改为正在装潢根据equipmentSheetNo
        equipmentSheetService.setEquipmentSheetType(equipmentSheetNo);


        return  "redirect:/handleEquipmentList?pageNo1="+pageNo1+"&pageNo2="+pageNo2;
    }



}
